//
//  StringHandleTool.swift
//  Wmsmobile
//
//  Created by xuNick on 16/9/27.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit

class StringHandleTool: NSObject {
    class func dictToDataSets(_ dictArr: [[String : String]]) -> (String){
        var result = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" id=\"NewDataSet\">\n<xs:element msdata:IsDataSet=\"true\" msdata:UseCurrentLocale=\"true\" name=\"NewDataSet\">\n<xs:complexType>\n<xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\">\n<xs:element name=\"collect\">\n<xs:complexType>\n<xs:sequence>\n"
        //<xs:element type="xs:string" minOccurs="0" name="FCODE" />
        for key in dictArr.first!.keys{
            result += "<xs:element type=\"xs:string\" minOccurs=\"0\" name=\"\(key)\" />\n"
        }
        result += "</xs:sequence>\n</xs:complexType>\n</xs:element>\n</xs:choice>\n</xs:complexType>\n</xs:element>\n</xs:schema>\n<diffgr:diffgram xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\n<NewDataSet xmlns=\"\">\n"
        /*
         <collect msdata:rowOrder="0" diffgr:hasChanges="inserted" diffgr:id="collect1">
         <FCODE>20090824</FCODE>
         <FNAME>美赞臣奶粉</FNAME>
         <FQPCSTR>1*10</FQPCSTR>
         <FQPC>10</FQPC>
         <FMUNIT>盒</FMUNIT>
         </collect>
         */
        var i = 0
        for item in dictArr {
            result += "<collect msdata:rowOrder=\"\(i)\" diffgr:hasChanges=\"inserted\" diffgr:id=\"collect\(i)\">\n"
            result += dictToString(item as NSDictionary)
            result += "</collect>\n"
            i += 1
        }
        result += "</NewDataSet>\n</diffgr:diffgram>"
        return result
        /*
         <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
         <xs:element msdata:IsDataSet="true" msdata:UseCurrentLocale="true" name="NewDataSet">
         <xs:complexType>
         <xs:choice minOccurs="0" maxOccurs="unbounded">
         <xs:element name="collect">
									<xs:complexType>
         <xs:sequence>
         <xs:element type="xs:string" minOccurs="0" name="货品GID" />
         <xs:element type="xs:string" minOccurs="0" name="货品代码" />
         <xs:element type="xs:string" minOccurs="0" name="货品名称" />
         <xs:element type="xs:string" minOccurs="0" name="复查件数" />
         <xs:element type="xs:string" minOccurs="0" name="件数" />
         <xs:element type="xs:string" minOccurs="0" name="复查数量" />
         <xs:element type="xs:string" minOccurs="0" name="数量" />
         <xs:element type="xs:string" minOccurs="0" name="规格" />
         <xs:element type="xs:string" minOccurs="0" name="规格数量" />
         <xs:element type="xs:string" minOccurs="0" name="拣货位" />
         <xs:element type="xs:string" minOccurs="0" name="行号" />
         </xs:sequence>
									</xs:complexType>
         </xs:element>
         </xs:choice>
         </xs:complexType>
         </xs:element>
         </xs:schema>
         <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
         <NewDataSet xmlns="">
         <collect msdata:rowOrder="0" diffgr:hasChanges="inserted" diffgr:id="collect1">
         <货品GID>1000010</货品GID>
         <货品代码>1000010,1112009,20090824,6901294179608</货品代码>
         <货品名称>美赞臣奶粉 400g</货品名称>
         <复查件赞臣奶粉 400g</货品名称>
         <复查件\346\225数>0</复查件数>
         <件数>3</件数>
         <复查数量>0</复查数量>
         <数量>30</数量>
         <规格>1*10</规格>
         <规格数量>10</规格数量>
         <拣货位>35010479</拣货位>
         <行号>1</行号>
         </collect>
         </NewDataSet>
         </diffgr:diffgram>
         */
    }
    //MARK: - 字典转XML键值对
    class func dictToString(_ dict:NSDictionary) -> String{
        let string = NSMutableString()
        for key in dict.allKeys{
            string.append("<\(key)>\(dict.object(forKey: key)!)</\(key)>\n")
        }
        return string.copy() as! String
    }
}
